<template>
  <div class>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <div class="t-left">
        <el-button type="primary">添加角色</el-button>
      </div>

      <!-- 表格 -->
      <el-table :data="rolesList" style="width: 100%;margin-top:15px;" stripe border>
        <el-table-column type="expand">
          <!-- 下拉区域 -->
          <template slot-scope="scope">
            <el-row
              v-for="(item1,index1) in scope.row.children"
              :key="item1.id"
              :class="[index1==0?'':'bdt','v-center']"
            >
              <el-col :span="8">
                <el-tag type="warning" closable @close="deleteRolesRights">{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="16">
                <el-row
                  v-for="(item2,index2) in item1.children"
                  :key="item2.id"
                  :class="[index2==0?'':'bdt','v-center']"
                >
                  <el-col :span="12">
                    <el-tag type="info" closable @close="deleteRolesRights">{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="12">
                    <el-tag
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      closable
                      @close="deleteRolesRights(scope.row,item3.id)"
                    >{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称" width="180"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作" width="300">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="showSetRightsDia(scope.row)"
            >分配权限</el-button>
            <!-- {{scope.row}} -->
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分配权限弹窗 -->
    <el-dialog
      title="分配角色权限"
      :visible.sync="setRightsdialogVisible"
      width="40%"
      @close="resetSetRightsDia"
    >
      <!-- 树形下拉菜单区域 -->
      <!-- 
        data是数据源
        props显示配置项
        show-checkbox显示复选框
        node-key选中复选框当前的属性值
      -->

      <el-tree
        :data="rightsList"
        :props="treeProps"
        show-checkbox
        node-key="id"
        ref="treeRef"
        default-expand-all
        :default-checked-keys="selectedKeyArr"
      ></el-tree>

      <!-- 按钮区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightsdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitSetRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      rolesList: [],
      // 分配角色显示/隐藏
      setRightsdialogVisible: false,
      // 所有权限树形结构数据
      rightsList: [],
      // 树形结构props配置项
      treeProps: {
        label: "authName",
        children: "children",
      },
      // 默认选中的复选框的id值
      selectedKeyArr: [],
      // 点击分配权限时的角色id
      roleId: null,
    };
  },
  created() {
    this.getRolesList();
  },
  mounted() {},
  watch: {},
  computed: {},
  methods: {
    getRolesList() {
      this.$axios.get("/roles").then((val) => {
        // console.log(val);
        if (val.meta.status != 200) {
          return this.$message.error(val.meta.msg);
        }
        this.rolesList = val.data;
      });
    },
    // 点击删除权限tag
    deleteRolesRights(role, id) {
      // console.log(role);
      this.$axios.delete("roles/" + role.id + "/rights/" + id).then((val) => {
        // console.log(val);
        if (val.meta.status !== 200) {
          return this.$message.error(val.meta.msg);
        }
        // 重新渲染表单,这里会把合上扩展行
        // this.getRolesList()
        // 因为接口给我们返回了当前角色下的最新数据
        role.children = val.data;
      });
    },
    // 点击分配权限按钮
    showSetRightsDia(role) {
      // 获取所有权限的树形数据
      this.$axios.get("rights/tree").then((val) => {
        // console.log(val);
        if (val.meta.status !== 200) {
          return this.$message.error(val.meta.msg);
        }
        this.rightsList = val.data;
      });
      // 调用递归函数将三级列表的id值放到selectedKeyArr数组里
      this.getThird(role, this.selectedKeyArr);
      // 打开对话框
      this.setRightsdialogVisible = true;
      // 将角色id保存一下
      this.roleId = role.id;
    },
    // 定义一个递归函数拿到第三级列表的id值
    getThird(node, arr) {
      if (!node.children) {
        return arr.push(node.id);
      }
      //递归调用
      node.children.forEach((item) => this.getThird(item, arr));
    },
    // 关闭分配权限窗口执行的事件
    resetSetRightsDia() {
      this.selectedKeyArr = [];
    },
    // 提交设置权限按钮
    submitSetRights() {
      // 将每次点选后，当前选中的id和半选中的id全部给到selectedKeyArr
      // getHalfCheckedKeys   getCheckedKeys
      this.selectedKeyArr = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys(),
      ];
      // selectedKeyArr转换成,逗号分割的字符串
      let str = this.selectedKeyArr.join(",");
      // 发请求
      this.$axios
        .post("roles/" + this.roleId + "/rights", {
          rids: str,
        })
        .then((val) => {
          console.log(val);
          if (val.meta.status != 200) {
            return this.$message.error(val.meta.msg);
          }
          // 再获取一遍角色信息
          this.getRolesList();
        });
      this.setRightsdialogVisible = false;
    },
  },
};
</script>
<style lang="css" scoped>
.t-left {
  text-align: left;
}
.el-tag {
  margin: 8px;
}
.bdt {
  border-top: 1px solid #eee;
}
.v-center {
  display: flex;
  align-items: center;
}
</style>